package ru.ifmo.cppsc.syntactic;

import ru.ifmo.cppsc.grammar.AttributedSymbol;
import ru.ifmo.cppsc.grammar.Production;

import java.util.Stack;

/**
 * Author: Olga Komaleva
 * Date: Jun 21, 2008
 */

public class TreeNode {
    private final AttributedSymbol symbol;
    private final Stack<TreeNode> children;
    private final Production p;
    private TreeNode parent;

    public TreeNode(AttributedSymbol symbol, Stack<TreeNode> children, TreeNode parent, Production p) {
        this.symbol = symbol;
        this.children = children;
        this.parent = parent;
        this.p = p;
    }

    public AttributedSymbol getAttrSymbol() {
        return symbol;
    }

    public TreeNode[] getChildren() {
        return children.toArray(new TreeNode[children.size()]);
    }

    public TreeNode getParent() {
        return parent;
    }

    public void setParent(TreeNode node) {
        parent = node;
    }

    public Production getProduction() {
        return p;
    }

    public TreeNode getChild(int index) {
        return children.get(index);
    }

    public String getType() {
        return symbol.getType();
    }

    public void setType(String type) {
        symbol.setType(type);
    }

    public String getIdValue() {
        return symbol.getIdValue();
    }

    public String getIdType() {
        return symbol.getIdType();
    }

    public static void printTree(TreeNode root) {
        if ((root.children != null) && (root.children.size() > 0)) {
            TreeNode[] chs =root.children.toArray(new TreeNode[root.children.size()]);
            for (int i = 0; i < chs.length; i++) {

                System.out.print(i + " ");
                printTree(chs[i]);
                System.out.print("; ");
            }
            System.out.println(" ");
        }

        System.out.print(root.symbol.getSymbol().getName());
    }
}
